package com.payment.validation;

import java.util.Calendar;
import javax.portlet.ActionRequest;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.Validator_IW;

public class CardValidation {
	
	public static final int DEFAULT = 0;
	public static final int VISA = 1;
	public static final int MASTERCARD = 2;
	public static final int DISCOVER = 3;
	public static final int AMEX = 4;
	
	public static int convertToInt(String val){
		if(!"".equals(val))
			return Integer.parseInt(val);
		else
			return 0; 
	} 
	
	/*** Credit Card  Validation ****/
	
	public static boolean checkDateOfExp(String mm, String yy) {
		Calendar cal = Calendar.getInstance();
		if (cal.get(Calendar.YEAR) >= convertToInt(yy)
				&& cal.get(Calendar.MONTH) >= convertToInt(mm))
			return false;
		else
			return true;
	}

	public static boolean isEmptyCard(ActionRequest actionRequest,
			String cardFirstName, String cardLastName, String cardNumber,
			String CVV, String expMM, String expYY) {
		boolean flag = true;
		
	Validator_IW numberValidation = Validator_IW.getInstance();

		if (cardFirstName == "" || cardFirstName == null) {
			SessionErrors.add(actionRequest, "firstKey");
			flag = false;
		} else if (cardLastName == "" || cardLastName == null) {
			SessionErrors.add(actionRequest, "lastKey");
			flag = false;
		}else if( !numberValidation.isDigit(cardNumber) || !numberValidation.isLUHN(cardNumber) || !numberValidation.isNumber(cardNumber) ){
			SessionErrors.add(actionRequest, "cardNumKey");
			flag=false;
		}else if (expMM.isEmpty() || expYY.isEmpty()) {
			SessionErrors.add(actionRequest, "emptyDate");
			flag = false;
		}else if (!checkDateOfExp(expMM, expYY)) {
			SessionErrors.add(actionRequest, "expDate");
			flag = false;
		} else if (CVV.length() != 3) {
			SessionErrors.add(actionRequest, "cvvKey");
			flag = false;
		}
		return flag;
	}

	public static int identifyCardType(String cardNumber) {

		if (cardNumber.startsWith("4") && (cardNumber.length() <= 13 || cardNumber.length() >= 16)) {
			return VISA;
		} else if (cardNumber.startsWith("51") || cardNumber.startsWith("52")
				|| cardNumber.startsWith("53") || cardNumber.startsWith("54")
				|| cardNumber.startsWith("55") && (cardNumber.length() == 16)) {
			return MASTERCARD;
		} else if (cardNumber.startsWith("6011") && (cardNumber.length() == 16)) {
			return DISCOVER;
		} else if (cardNumber.startsWith("34") || cardNumber.startsWith("35")
				|| cardNumber.startsWith("36") || cardNumber.startsWith("37")
				&& (cardNumber.length() == 15)) {
			return AMEX;
		} else {
			return DEFAULT;
		}
	}

	/*** PaypalBucket Validation ****/

	public static boolean isValidInfo(ActionRequest actionRequest,
			String title, String apiUserName, String apiPassword,
			String apiSignature) {
		int passLength = apiPassword.length();
		if (title == "") {
			SessionErrors.add(actionRequest, "titleKey");
			return false;
		} else if (apiUserName == "") {
			SessionErrors.add(actionRequest, "uNameKey");
			return false;
		} else if (apiPassword == "") {
			SessionErrors.add(actionRequest, "passKey");
			return false;
		} else if (passLength < 6) {
			SessionErrors.add(actionRequest, "minLengthKey");
			return false;
		} else if (passLength > 25) {
			SessionErrors.add(actionRequest, "maxLengthKey");
			return false;
		} else if (apiSignature == "") {
			SessionErrors.add(actionRequest, "signKey");
			return false;
		} else
			return true;
	}

}
